/*
 * @desc:
 * @Author: 钟强
 * @Date: 2021-12-10 11:24:49
 * @LastEditors: 钟强
 * @LastEditTime: 2022-05-26 09:04:50
 */

// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
console.log(process.env.NODE_ENV)
const path = require('path')
const resolve = dir => path.join(__dirname, dir)

const isProduction = process.env.NODE_ENV === 'production'

const outPlugins = isProduction ? [] : []

const optimizationConfig = {
  splitChunks: {
    chunks: 'initial',
    minSize: 30000,
    maxSize: 0,
    minChunks: 2,
    maxAsyncRequests: 6,
    maxInitialRequests: 4,

    cacheGroups: {
      common: {
        // 抽取所有入口页面都需要的公共chunk
        name: 'chunk-common',
        chunks: 'initial',
        minChunks: 2,
        maxInitialRequests: 5,
        minSize: 0,
        priority: 1,
        reuseExistingChunk: true,
        enforce: true
      },

      // 将体积较大的包单独分离出来，减少vendor的体积，加快首屏速度
      vendors: {
        name: 'chunk-vendors', // enforce默认为false下不能直接设置name值，否则打包会失败
        test: /[\\/]node_modules[\\/]/,
        priority: -10, // 表示缓存的优先级；
        enforce: true,
        reuseExistingChunk: true
      },
      icons: {
        minChunks: 1,
        name: 'chunk-icons',
        priority: 4,
        test: /[\\/]src[\\/]style[\\/]font/
      },
      vueUI: {
        name: 'vueUI',
        test: /[\\/]node_modules[\\/]vue[\\/]/,
        priority: 4,
        chunks: 'all',
        reuseExistingChunk: true,
        enforce: true
      },
      vantUI: {
        name: 'chunk-vant-ui',
        // 最大12KB，这里可以自定义，也可以不加
        // maxSize: 10240,
        // minSize: 1000,
        // 正则表达式来获取node_modules/element-plus/ 下的模块
        test: /[\\/]node_modules[\\/]vant[\\/]/,
        // 所有的chunks都被拆分，异步或同步
        chunks: 'all',
        priority: 3,
        reuseExistingChunk: true,
        // 这里会忽略外部的minSize等，但内部的minSize仍然会被遵守
        enforce: true
      },
      styles: {
        name: 'styles',
        test: /\.(sa|sc|c)ss$/,
        chunks: 'all',
        enforce: true
      },
      runtimeChunk: {
        name: 'manifest'
      },
      default: {
        minChunks: 2,
        priority: -20,
        reuseExistingChunk: true
      }
    }
  },
  minimizer: [ // 文件压缩
    // new UglifyJsPlugin({
    //   test: /\.js(\?.*)?$/i,
    //   parallel: true,
    //   uglifyOptions: {
    //     compress: {
    //       drop_console: false,
    //       drop_debugger: true
    //     }
    //   }
    // })
  ]

}
// https://cli.vuejs.org/zh/guide/webpack.html#%E7%AE%80%E5%8D%95%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%B9%E5%BC%8F
module.exports = {
  transpileDependencies: true, // babel-loader 需要对node_modules中的文件进行转义
  productionSourceMap: false,
  indexPath: 'load.html', // dmapp 默认读取是load
  publicPath: './', // 改为相对路径
  outputDir: 'cloud_disk',
  devServer: {
    // 配置反向代理
    // proxy: {
    //   // 当地址中有/api的时候会触发代理机制
    //   '/api': {
    //     // 要代理的服务器地址  这里不用写 api
    //     target: 'https://api.admin.com',
    //     changeOrigin: true // 是否跨域
    //   }
    // }
  },
  chainWebpack (config) {
    config
      .plugin('html')
      .tap(args => {
        args[0].title = '零极云盘'
        return args
      })
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
  },
  configureWebpack: {
    plugins: [
      ...outPlugins
    ],
    optimization: isProduction ? optimizationConfig : {}
  }
}
